package com.share2.thread2;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Demo5_Executors {

	/**
	 * public static ExecutorService newFixedThreadPool(int nThreads)
	 * public static ExecutorService newSingleThreadExecutor()
	 * @throws ExecutionException 
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		ExecutorService pool = Executors.newFixedThreadPool(2);//创建线程池
//		pool.submit(new MyRunnable());				//将线程放进池子里并执行
		Future<?> submit = pool.submit(new MyRunnable2());       //提交一个 Runnable 任务用于执行，并返回一个表示该任务的 Future。
		System.out.println("main-前");
		Object object = submit.get();
		System.out.println(object);
		System.out.println("main-后");
		pool.shutdown();							//关闭线程池---启动一次顺序关闭，执行以前提交的任务，但不接受新任务。
	}
	
	
}

class MyRunnable2 implements Runnable{

	@Override
	public void run() {
		try {
			Thread.sleep(5000);
			System.out.println("myRunnable2");
			
			
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}
